package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.entity.Setmeal;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Override
    public List<Dish> getDishList(Long categoryId,String name) {
        List<Dish> dishList = dishMapper.getDishList(categoryId,name);
        return dishList;
    }

    @Override
    @Transactional
    public void addDishWithFlavors(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        dishMapper.addDish(dish);
        Long id = dish.getId();
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if(flavors!= null && flavors.size() > 0){
            flavors.forEach(flavor -> {
                flavor.setDishId(id);
            });
            dishMapper.addFlavors(flavors);
        }
    }

    @Override
    public PageResult getDishPage(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage() ,dishPageQueryDTO.getPageSize());
        Page<DishVO> dishPage = dishMapper.getDishPage(dishPageQueryDTO);
        return new PageResult(dishPage.getTotal(), dishPage.getResult());
    }

    @Override
    @Transactional
    public void deleteDish(List<Long> ids) {
        //首先看菜品是否为起售状态，如果是，则不能删除
        List<Long> dishStatusUseBatch = dishMapper.findDishStatusUseBatch(ids);
        for(Long id : dishStatusUseBatch){
            if (id.intValue()==StatusConstant.ENABLE){
                throw  new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }
        //再看菜品是否有关联的套餐，如果有，则不能删除
        List<Long> setMealIdsByDishIdUseBatch = setmealMapper.getSetMealIdsByDishIdUseBatch(ids);
        if (setMealIdsByDishIdUseBatch!= null && setMealIdsByDishIdUseBatch.size() > 0){
                throw  new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }
        //删除菜品
        dishMapper.deleteDishByDishIdsBatch(ids);

        //删除菜品对应的口味
        dishMapper.deleteFlavorsByDishIdsBatch(ids);
    }

    @Override
    @Transactional
    public DishVO getDishById(Long id) {
       Dish dish = dishMapper.getDishById(id);
       DishVO dishVO = new DishVO();
       BeanUtils.copyProperties(dish, dishVO);
        List<DishFlavor> flavors = dishMapper.getDishFlavors(id);
        dishVO.setFlavors(flavors);
        return dishVO;
    }

    @Override
    @Transactional
    public void updateDish(DishDTO dishDTO) {
        Dish dish = Dish.builder().build();
        BeanUtils.copyProperties(dishDTO, dish);
        dishMapper.updateDish(dish);
        dishMapper.deleteFlavorsByDishIdsBatch(List.of(dish.getId()));
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if(flavors != null && flavors.size() > 0){
            flavors.forEach(flavor -> {
                flavor.setDishId(dishDTO.getId());
            });
            dishMapper.addFlavors(flavors);
        }

    }

    @Override
    public List<DishVO> getDishVoByCategoryId(Dish dish) {
        List<Dish> dishList = dishMapper.getDishWithEnableStatusByCategoryId(dish);
        List<DishVO> dishVOList = new ArrayList<>();
        for(Dish d : dishList){
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d, dishVO);
            List<DishFlavor> flavors = dishMapper.getDishFlavors(d.getId());
           dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }
        return dishVOList;
    }

    @Override
    @Transactional
    public void updateDishStatus(Integer status, Integer id) {
        System.out.println("传来的status:" + status);
        Dish dish = Dish.builder().id(id.longValue()).status(status).build();
        dishMapper.updateDish(dish);
        if(status == StatusConstant.DISABLE){
            List<Long> setMealIdsByDishIdUseBatch = setmealMapper.getSetMealIdsByDishIdUseBatch(List.of(id.longValue()));
               if (setMealIdsByDishIdUseBatch!= null && setMealIdsByDishIdUseBatch.size() > 0){
                   for(Long setMealId : setMealIdsByDishIdUseBatch){
                       Setmeal setmeal = Setmeal.builder().id(setMealId).status(StatusConstant.DISABLE).build();
                       setmealMapper.updateSetmeal(setmeal);
                   }

               }
        }
    }


}
